﻿using System.Web.Mvc;
using FoodBank.Core.Validation;

namespace FoodBank.Web
{
    /// <summary>
    /// Wrapper for MVC's ModelState to do validation in service layer.
    /// </summary>
    public class ModelStateWrapper : IValidationDictionary
    {
        private readonly ModelStateDictionary _modelState;

        /// <summary>
        /// Initializes a new instance of the <see cref="ModelStateWrapper"/> class.
        /// </summary>
        /// <param name="modelState">State of the model.</param>
        public ModelStateWrapper(ModelStateDictionary modelState)
        {
            _modelState = modelState;
        }

        /// <summary>
        /// Adds the error.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="errorMessage">The error message.</param>
        public void AddError(string key, string errorMessage)
        {
            _modelState.AddModelError(key, errorMessage);
        }

        /// <summary>
        /// Gets a value indicating whether this instance is valid.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </value>
        public bool IsValid
        {
            get { return _modelState.IsValid; }
        }
    }
}